package controller.admin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.admin.dao.AdminDao;
import model.admin.dto.AdminDto;
import model.class1.dao.Class1Dao;
import model.member.dao.MemberDao;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import common.SearchDto;

public class AdminController extends MultiActionController {
	private Map<String,Object> map;
	private SearchDto search;
	private HttpSession session;
	private AdminDto dto;
	
	private MemberDao memberDao;
	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}
	
	private AdminDao adminDao;
	public void setAdminDao(AdminDao adminDao) {
		this.adminDao = adminDao;
	}
	private Class1Dao class1Dao;
	public void setClass1Dao(Class1Dao class1Dao) {
		this.class1Dao = class1Dao;
	}
	
	/////////////////////////
	public void auto_level_update(HttpServletRequest request, HttpServletResponse response) throws Exception{
		dto = new AdminDto();
		dto.setCd_date(request.getParameter("cd_date"));
		dto.setCd_rate_avg1(Integer.parseInt(request.getParameter("cd_rate_avg1")));
		dto.setCd_rate_avg2(Integer.parseInt(request.getParameter("cd_rate_avg2")));
		dto.setCd_rate_avg3(Integer.parseInt(request.getParameter("cd_rate_avg3")));
		dto.setCd_rate_avg4(Integer.parseInt(request.getParameter("cd_rate_avg4")));
		dto.setCd_rate_sum1(Integer.parseInt(request.getParameter("cd_rate_sum1")));
		dto.setCd_rate_sum2(Integer.parseInt(request.getParameter("cd_rate_sum2")));
		dto.setCd_rate_sum3(Integer.parseInt(request.getParameter("cd_rate_sum3")));
		dto.setCd_rate_sum4(Integer.parseInt(request.getParameter("cd_rate_sum4")));	
		
		adminDao.autoLevelUpdate(dto);		
	}
	
	public ModelAndView admin_class_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		session = request.getSession();
		String level = session.getAttribute("level").toString();
		if(!level.equals("0")){
			return new ModelAndView("/index.jsp");
		}
		search = new SearchDto();
		search.setLimit(30);
		search.setOffset(Integer.parseInt(request.getParameter("offset")));
		search.setOp_date(request.getParameter("op_date"));
		String direction = request.getParameter("op_direction");
		if(direction.equals("gt")){
			search.setOp_direction(">=");			
		}else{
			search.setOp_direction("<");
		}
		
		search.setOp_order(request.getParameter("op_order"));
		search.setOp_state(request.getParameter("state"));
		
		map = new HashMap<String, Object>();
		map.put("list", class1Dao.adminClass1List(search));
		
		return new ModelAndView("/admin/admin_class1_result.jsp","map",map);
	}
	
	public void admin_class_autoClose(HttpServletRequest request, HttpServletResponse response) throws Exception{
		adminDao.autoClassClose();
	}
	
	public void admin_class_Close(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int idx = Integer.parseInt(request.getParameter("idx"));
		adminDao.classClose(idx);
		//adminDao.resistClose(idx);
	}
	
	public void admin_auto_class_done(HttpServletRequest request, HttpServletResponse response) throws Exception{
		adminDao.autoClassDone();		
	}
	
}
